import threading
import random
import time

lock = threading.Lock()             #加锁

list1=[0]*10
print(list1)
def task1():
    # 获取线程锁，如果线程已经上锁，则等待锁的释放（阻塞）
    lock.acquire()              # 阻塞
    for i in range(len(list1)):
        list1[i]=1
        time.sleep(0.5)
    lock.release()              # 释放

def task2():
    # 获取线程锁，如果线程已经上锁，则等待锁的释放（阻塞）
    lock.acquire()              # 阻塞
    for i in range(len(list1)):
        print("---->",list1[i])
        time.sleep(0.5)
    lock.release()              # 释放

if __name__=="__main__":
    t1=threading.Thread(target=task1)
    t2=threading.Thread(target=task2)
    t2.start()
    t1.start()



    t1.join()
    t2.join()
    print(list1)
